package com.question.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.util.AjaxResponse;
import com.common.util.JpaPageable;
import com.common.util.TablePage;
import com.question.entity.Explanation;
import com.question.entity.Judgment;
import com.question.entity.dto.ExplanationDTO;
import com.question.entity.dto.JudgmentDTO;
import com.question.service.IExplService;
import com.user.entity.dto.TeacherDTO;
import com.user.service.ITeacherService;

@Controller
@RequestMapping("/expl")
public class ExplanationController {
	@Autowired
	private IExplService explService;
	@Autowired
	private ITeacherService teacherService;
	
	@RequestMapping("/findPage")
	public @ResponseBody TablePage<ExplanationDTO> findPage(JpaPageable pageable,HttpSession session,String str,Long subject_id){
		try {
			List<Long> ids = new ArrayList<Long>();
			
			TeacherDTO dto = (TeacherDTO) session.getAttribute("teacher");
			if(null!=dto)
				ids = teacherService.getSubjectsId(dto.getId());
			return explService.findPage(ExplanationDTO.getSpecification(ids,str,subject_id), pageable.getPageable());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody AjaxResponse saveOrUpdate(Explanation explanation,Long subjectId,String referenceStr) {
		try {
			explService.saveOrUpdate(explanation, subjectId, referenceStr);
			if(explService.isNumExist(explanation))
				return new AjaxResponse(false, "编号已存在!");
			if(explanation.getId()!=null&&explService.isUsing(explanation.getId()))
				return new AjaxResponse(false, "题目已被使用，不允许修改!");
			return new AjaxResponse(true, "操作成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败!");
		}
	}
	@RequestMapping("/delete")
	public @ResponseBody AjaxResponse delete(Long id) {
		try {
			if(explService.isUsing(id))
				return new AjaxResponse(false, "题目已被使用，不允许删除!");
			explService.delete(id);
			return new AjaxResponse(true, "已删除!");
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResponse(false, "删除失败!");
		}
	}
	
	@RequestMapping("/findRealPage")
	public @ResponseBody TablePage<Explanation> findRealPage(JpaPageable pageable,HttpSession session,String str,Long subject_id){
		try {
			List<Long> ids = new ArrayList<Long>();
			
			TeacherDTO dto = (TeacherDTO) session.getAttribute("teacher");
			if(null!=dto)
				ids = teacherService.getSubjectsId(dto.getId());
			return explService.findRealPage(ExplanationDTO.getSpecification(ids,str,subject_id), pageable.getPageable());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/findOne")
	public @ResponseBody Explanation findOne(Long id) {
		try {
			return explService.findOne(id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
}
